# Script to serve as ArcGIS tool  GUI frontend for plot_tiled.py
# uses a geoprocessor to print debug/error messages inside the ArcTool console
# calls a "backend" script
# CH June 2011
import sys


# Python 2.6 uses a different way to get the geoprocessing object
# than 2.5 (default for ArcGIS 9.3). Bail out for any other version of Python
import platform # for checking which python version we're running
python_version_string = platform.python_version()
print "Running Python version", python_version_string
if python_version_string[0:3] == "2.5":
    import arcgisscripting
    gp = arcgisscripting.create(9.3) # ArcGIS 9.3 geoprocessing
    #gp = arcgisscripting.create() # pre 9.3 version
elif python_version_string[0:3] == "2.6":
    try:
        import arcgisscripting # if you use python 2.6, are you on ArcGIS 9.3 or 10?
        gp = arcgisscripting.create(9.3) #use the 9.3 arcscripting (still works for 10)

    except:
        # if not, maybe your're on ArcGIS 9.3 but instead of python 2.5 you use python 2.6 (naughty, naughty ...)?
        # install pywin32, see http://sourceforge.net/projects/pywin32/files/ and use this:
        try:
            import win32com.client
            gp = win32com.client.Dispatch("esriGeoprocessing.GpDispatch.1")
        except:
            gp.AddMessage("Can't create geoprocessing object via python 2.6 in ArcGIS 9.3")
            sys.exit()
else:
    gp.AddMessage("Can't create geoprocessing object - bailing out!")
    sys.exit()

# XML log file in C:\Users\<user name>\AppData\Roaming\ESRI\ArcToolbox\History (Vista)
# or C:\Documents and Settings\<user name>\Application Data\ESRI\ArcToolbox\History (XP)
#gp.LogHistory = True

# if we have args (from the ArcGIS tool frontend), print them out and overwrite
# the defaults with them
args = sys.argv
#for i in range(1, len(args)):
#    gp.AddMessage("arg %i is %s" % (i, args[i])) # debug, show args in ArcGIS tool "terminal"


# call the backend with gp 
import plot_qt4_backend as plot
plot.main(args, gp)

#gp.AddMessage("Done with ArcGIS GUI script")

